package com.attms.service.impl;

import com.attms.dao.AbsentDao;
import com.attms.domain.Absent;
import com.attms.domain.Employee;
import com.attms.dao.EmployeeDao;
import com.attms.service.IEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Team
 * @since 2022-12-18
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private AbsentDao absentDao;
    @Override
    public Boolean signIn(Integer id) {
        Employee afterSignIn = new Employee();
        afterSignIn.setE_ID(id);
        LocalDateTime signInTime = LocalDateTime.now();
        afterSignIn.setE_SIGNINTIME(signInTime);
        afterSignIn.setE_STATE(2);
        return employeeDao.updateById(afterSignIn) > 0;
    }

    @Override
    public Boolean signOut(Integer id) {
        Employee afterSignOut = new Employee();
        afterSignOut.setE_ID(id);
        LocalDateTime signOutTime = LocalDateTime.now();
        afterSignOut.setE_SIGNOUTTIME(signOutTime);
        afterSignOut.setE_STATE(4);

        Employee employee = employeeDao.selectById(id);
        LocalDateTime signInTime = employee.getE_SIGNINTIME();
        Integer orignalAbsHour = employee.getE_ABSHOUR();
        Duration duration = Duration.between(signInTime, signOutTime);
        Integer workHour = (int) (8 - duration.toHours());
        if(workHour > 0){
            afterSignOut.setE_ABSHOUR(workHour + orignalAbsHour);
        }

        return employeeDao.updateById(afterSignOut) > 0;
    }

    @Override
    public Boolean askForLeave(Absent absent) {
//        上交的申请包括：请假类型、请假原因、请假起、止时间、请假员工id
//        自动设置请假状态为0（部门经理审批中）
//        类型0为因公请假，不扣钱
//        类型为1-8分别是年假、病假、婚假、产检假、产假、哺乳假、陪产假、事假
//        首先无性别：年假1、病假2、婚嫁3、事假8
//        男：陪产假7
//        女：产检假4、产假5、哺乳假6

        int vocationType = absent.getA_TYPE();

        int  askDays = Period.between(absent.getA_ENDTIME(), absent.getA_STTIME()).getDays();
        System.out.println(askDays + ">>>>>>>");
        Employee nowPerson = employeeDao.selectById(absent.getE_ID());
        int gerder =nowPerson.getE_GENDER();
        //状态默认为0
        absent.setA_STATE(0);
        if(vocationType==0){//因公请假
            absentDao.insert(absent);
        }else if(vocationType == 1){//年假
            //根据工龄看要不要自动驳回,感觉应该在人事那里就算好了
            int remainYearLeave = nowPerson.getE_RYEAR();
            if(remainYearLeave >= askDays)absentDao.insert(absent);
            else return false;//所申请的不扣薪的假期已经申请完，请按照事假申请
        }else if(vocationType == 2){//病假
            int remainIll = nowPerson.getE_RILL();
            if(remainIll >= askDays)
                absentDao.insert(absent);
            else return false;
        }else if(vocationType == 3){//婚假，3天或者10天在人事部算
            int remainWedding = nowPerson.getE_RWEDDING();
            if(remainWedding >= askDays)
                absentDao.insert(absent);
            else
                return false;
        }else if(vocationType == 8){//事假
            absentDao.insert(absent);
        }else if(vocationType == 7){//陪产假
            int remainPaterrnity = nowPerson.getE_RPATERNITY();
            if(gerder == 1 && remainPaterrnity >= askDays)absentDao.insert(absent);
            else return false;
        }else{
            if(gerder == 1)return false;
            if(vocationType == 4){//不限次数的产检假
                absentDao.insert(absent);
            }else if(vocationType == 5){//没办法提前设置限制的产假
                absentDao.insert(absent);
            }else{//哺乳假
                int mins = (int)Duration.between(absent.getA_STTIME(),absent.getA_ENDTIME()).toMinutes();
                int remainmins = nowPerson.getE_RLACTATION();
                if(remainmins>=mins)absentDao.insert(absent);
                else return false;
            }
        }
        return true;
    }

    @Override
    public List<Employee> findRestLeave(Integer id) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //年假、病假、哺乳假、产假、陪产假、婚假
        wrapper.eq(Employee::getE_ID,id);
//                .select(Employee::getE_RYEAR,Employee::getE_RILL,Employee::getE_RLACTATION,Employee::getE_RMATERNITY,Employee::getE_RPATERNITY,Employee::getE_RWEDDING);
        return employeeDao.selectList(wrapper);
    }
}
